package core

import (
	"strings"
)

// todo 二级子菜单
var navsList []string
var newsHtml string

func (h *HTML) UI() *HTML {
	h.Con = `<html lang="@{lang}">
				<head>
					<meta charset="UTF-8">
					<meta http-equiv="X-UA-Compatible" content="IE=edge">
					<meta name="viewport" content="width=device-width, initial-scale=1.0">
					<title>@{title}</title>
					<meta name="keywords" content="@{keywords}" />
					<meta name="description" content="@{description}" />
					<link rel="stylesheet" href="/assets/css/prism.css" />
					<link rel="shortcut icon" href="@{ico}" type="image/x-icon" />
					@{link}
				</head>
				<body>
					<header>@{menu}</header>
					<div class="main">
						<aside>@{aside}</aside>
						<article> @{body}</article>
					</div>
					<footer>@{footer}</footer>
					@{script}
					<script src="/assets/js/prism.js"></script>
					<script src="/assets/js/mermaid.min.js"></script>
				</body>
			</html>`
	h.head().menu().nav().body().news().aside().footer()
	return h
}

/** 加载顶部导航栏 */
func (h *HTML) head() *HTML {
	h.Con = strings.Replace(h.Con, "@{lang}", h.HF.Lang, -1)
	h.Con = strings.Replace(h.Con, "@{title}", h.HF.Title, -1)
	h.Con = strings.Replace(h.Con, "@{keywords}", h.HF.KeyWords, -1)
	h.Con = strings.Replace(h.Con, "@{description}", h.HF.Description, -1)
	h.Con = strings.Replace(h.Con, "@{ico}", h.HF.Ico, -1)
	link := ""
	if len(h.HF.Style) > 0 {
		for _, v := range h.HF.Style {
			link += `<link rel="stylesheet" href="` + v + `" />`
		}
	}
	h.Con = strings.Replace(h.Con, "@{link}", link, -1)

	return h
}

// * 每个页面都加载顶部导航栏
func (h *HTML) menu() *HTML {
	h.Con = strings.Replace(h.Con, "@{menu}", navTemp("index", "row"), 1)
	return h
}

/*
 * 获取config.ini中要显示的分类栏目
 */
func (h *HTML) nav() *HTML {
	html_string := ``
	for _, v := range navsList {
		html_string += `<li><a href="/` + v + `">` + v + `</a></li>`
	}
	h.Con = Replace(h.Con, "@{nav}", html_string)
	return h
}

func (h *HTML) body() *HTML {
	h.Con = Replace(h.Con, "@{body}", h.Md)
	h.Con = strings.ReplaceAll(h.Con, ".md", ".html")
	return h
}

func (h *HTML) footer() *HTML {
	scr := ""
	if len(h.HF.Script) > 0 {
		for _, v := range h.HF.Script {
			scr += `<script src="` + v + `" ></script>`
		}
	}
	h.Con = strings.Replace(h.Con, "@{script}", scr, -1)
	footer := `<nav><ul class="nav flex-column"><li class="nav-item">友情链接</li><li class="nav-item"><a href="https://uiverse.io/">新拟态UI</a></li><li class="nav-item"><a href="https://cn.vuejs.org/">Vue</a></li><li class="nav-item"><a href="https://www.bootcss.com/">Bootstrap</a></li></ul></nav><div><span>Copyright &copy; 2022 凡心至臻</span><a href="https://beian.miit.gov.cn/#/Integrated/index">豫ICP备18022265号</a></div>`
	h.Con = strings.Replace(h.Con, "@{footer}", footer, -1)
	return h
}

func (h *HTML) aside() *HTML {
	h.Con = Replace(h.Con, "@{aside}", "")
	return h
}

func (h *HTML) news() *HTML {
	if strings.Index(h.Con, "@{news}") >= 0 {
		h.Con = Replace(h.Con, "@{news}", newsHtml)
	}
	return h
}
